package cn.lbd.pseudoasynchronous;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.concurrent.TimeUnit;

public class Client2 {
    private static String ADDRESS = "127.0.0.1";
    private static int PORT = 8765;

    public static void main(String[] args) {
        Socket socket = null;
        BufferedReader in = null;
        PrintWriter out = null;

        try {
            //客户端上会有一个socket实例
            //客户端的套接字必须首先描述它要连接的服务器的套接字（ip + port），对应ServerSocket的port
            socket = new Socket(ADDRESS,PORT);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            //写出的时候记得刷新！！！
            out = new PrintWriter(socket.getOutputStream(),true);
            try {
                TimeUnit.SECONDS.sleep(2l);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //向服务器端发送数据
            out.println("接收到客户端2的请求数据...");
            out.println("孙笑川天皇...");
            String response = in.readLine();
            System.out.println("Client2：" + response);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (in !=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null){
                out.close();
            }
            if (socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            socket = null;
        }
    }

}
